<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
    public function design() {
        $this->template->set_layout('orpms');
        $this->template->set_partial('styles','partials/styles');
        $this->template->set_partial('scripts_a','partials/scripts_a');
        $this->template->set_partial('header','partials/header');
        $this->template->set_partial('subnav','partials/subnav');
        $this->template->set_partial('navigation','partials/navigation');
        $this->template->set_partial('scripts_b','partials/scripts_b');
        $this->template->set_partial('footer','partials/footer');
    }
    
    public function um() {
        if($this->session->userdata('username')) {
            $privlist = getprivileges($this->session->userdata('role_id'));
            
            if(!in_array(6,$privlist)) {
                redirect(base_url().'error/errors?code=403','refresh');
            }
            
            if(!isset($_GET['action']) OR $_GET['action'] != 'add') {
                if(!isset($_GET['page']) AND (!isset($_GET['action']) OR $_GET['action'] != 'add')) {
                    redirect(base_url().'admin/um?page=1','refresh');
                }
                
                $select = array(
                    'account_id',
                    'username',
                    'last_login',
                    'tbl_profile.profile_id',
                    'f_name',
                    'l_name',
                    'gender',
                    'birthday',
                    'avatar',
                    'tbl_roles.role_id',
                    'role_name',
                    'level'
                );
                
                $table = array(
                    'tbl_account',
                    'tbl_profile',
                    'tbl_roles'
                );
                
                if(isset($_GET['keywords'])) {
                    //$keywords = $_GET['keywords'];
                    $keywords = preg_split('/[\s]+/',$_GET['keywords']);
                    $where[0] = array('tbl_profile.profile_id','=','tbl_account.profile_id',' AND ');
                    $where[1] = array('tbl_roles.role_id','=','tbl_account.role_id',' AND ');
                    
                    $count = 2;
                    foreach($keywords as $key) {
                        if(isset($_GET['searchby'])) {
                            switch($_GET['searchby']) {
                                case 'account_id':
                                    $field="tbl_account.account_id";break;
                                case 'username':
                                    $field="tbl_account.username";break;
                                case "fullname":
                                    $field="CONCAT(tbl_profile.f_name,tbl_profile.l_name)";break;
                                case "lastlogin":
                                    $field="tbl_account.last_login";
                            }
                            $where[$count] = array($field,' LIKE ','\'%'.$key.'%\'',' AND ');
                        } else {
                            $field="CONCAT(tbl_profile.f_name,tbl_profile.l_name)";
                            $where[$count] = array($field,' LIKE ','\'%'.$key.'%\'',' AND ');
                        }
                        $count++;
                    }
                } else {
                    $where[0] = array('tbl_profile.profile_id','=','tbl_account.profile_id',' AND ');
                    $where[1] = array('tbl_roles.role_id','=','tbl_account.role_id',' AND ');
                }
                
                if(isset($_GET['sortby'])) {
                    switch($_GET['sortby']) {
                        case 'account_id':
                            $sortby="tbl_account.account_id";break;
                        case 'username':
                            $sortby="tbl_account.username";break;
                        case "fullname":
                            $sortby="CONCAT(tbl_profile.f_name,tbl_profile.l_name)";break;
                        case "lastlogin":
                            $sortby="tbl_account.last_login";
                    }
                } else {
                    $sortby = "tbl_account.account_id";
                }
                
                if(isset($_GET['sorttype'])) {
                    switch($_GET['sorttype']) {
                        case 'asc':
                            $sorttype="ASC";
                            break;
                        default:
                            $sorttype="DESC";
                    }
                } else {
                    $sorttype = "DESC";
                }
                
                $this->load->model('msearch');
                $search = $this->msearch->search($select,$table,$where,$sortby,$sorttype,$_GET['page'],5);
                $data['userlist'] = $search['result'];
                $data['totalpage'] = $search['totalpage'];
            }
            
            $this->design();
            
            //Build
            if(!isset($_GET['action']) OR $_GET['action'] != 'add') {
                $this->template->title('User Management - ORPMS');
                $this->template->build('pages/admin/um',$data);
            } else if($_GET['action'] == 'add') {
                $this->load->model('mroles');
                $roles['roles'] = $this->mroles->getRoles();
                $this->template->title('User Management (New User) - ORPMS');
                $this->template->build('pages/admin/adduser',$roles);
            }
        } else {
            redirect(base_url().'account/login','refresh');
        }
    }
    
    public function akm() {
        if($this->session->userdata('username')) {
            $privileges = getprivileges($this->session->userdata('role_id'));
            if(in_array(5,$privileges)) {
                if(!isset($_GET['page'])) {
                    redirect(base_url().'admin/akm?page=1','refresh');
                }
                
                $select = array('accesskey_id','tbl_account.account_id','f_name','l_name','tbl_accesskey.key','tbl_accesskey.research_id');
                $table = array('tbl_accesskey','tbl_account','tbl_profile');
                
                if(isset($_GET['keywords'])) {
                    //$keywords = $_GET['keywords'];
                    $keywords = preg_split('/[\s]+/',$_GET['keywords']);
                    $where[0] = array('tbl_account.account_id','=','tbl_accesskey.account_id',' AND ');
                    $where[1] = array('tbl_profile.profile_id','=','tbl_account.profile_id',' AND ');
                    
                    $count = 2;
                    foreach($keywords as $key) {
                        if(isset($_GET['searchby'])) {
                            switch($_GET['searchby']) {
                                case 'access_key':
                                    $field="tbl_accesskey.key";break;
                                case 'fullname':
                                    $field="CONCAT(tbl_profile.f_name,tbl_profile.l_name)";
                            }
                            $where[$count] = array($field,' LIKE ','\'%'.$key.'%\'',' AND ');
                        } else {
                            $field="CONCAT(tbl_profile.f_name,tbl_profile.l_name)";
                            $where[$count] = array($field,' LIKE ','\'%'.$key.'%\'',' AND ');
                        }
                        $count++;
                    }
                } else {
                    $where[0] = array('tbl_account.account_id','=','tbl_accesskey.account_id',' AND ');
                    $where[1] = array('tbl_profile.profile_id','=','tbl_account.profile_id',' AND ');
                }
                
                if(isset($_GET['sortby'])) {
                    switch($_GET['sortby']) {
                        case 'access_key':
                            $sortby="tbl_accesskey.key";break;
                        case 'fullname':
                            $sortby="CONCAT(tbl_profile.f_name,tbl_profile.l_name)";
                    }
                } else {
                    $sortby = "accesskey_id";
                }
                
                if(isset($_GET['sorttype'])) {
                    switch($_GET['sorttype']) {
                        case 'asc':
                            $sorttype="ASC";
                            break;
                        default:
                            $sorttype="DESC";
                    }
                } else {
                    $sorttype = "DESC";
                }
                
                $this->load->model('msearch');
                $search = $this->msearch->search($select,$table,$where,$sortby,$sorttype,$_GET['page'],5);
                $data['keylist'] = $search['result'];
                $data['totalpage'] = $search['totalpage'];
                
                $this->design();
                $this->template->title('Access Key Management - ORPMS');
                $this->template->build('pages/admin/akm',$data);
            } else {
                redirect(base_url().'error/errors?code=403','refresh');
            }
        } else {
            redirect(base_url().'account/login','refresh');
        }
    }
    
    public function rm($sub) {
        if($this->session->userdata('username')) {
            $privlist = getprivileges($this->session->userdata('role_id'));
            if(in_array(7,$privlist)) {
                switch($sub) {
                    case 'researches':
                        break;
                    case 'section':
                        $this->load->model('mresearch');
                        if(isset($_GET['action'])) {
                            switch($_GET['action']) {
                                case 'update':
                                    if(isset($_GET['id'])) {
                                        $this->load->model('mresearch');
                                        $desc = $this->mresearch->getsectioninfo($_GET['id']);
                                        
                                        if($desc != null) {
                                            $this->design();
                                            $data['desc'] = $desc;
                                            $title = 'Research Sections (Update) - ORPMS';
                                            $page = 'pages/admin/updatesection';
                                        } else {
                                           redirect(base_url().'error/errors?code=404','refresh');  
                                        }
                                        
                                    } else {
                                        redirect(base_url().'error/errors?code=404','refresh');
                                    }
                                    break;
                                case 'add':
                                    $this->design();
                                    $data = null;
                                    $title = 'Research Sections (New) - ORPMS';
                                    $page = 'pages/admin/addsection';
                                    break;
                            }
                        } else {
                            $data['sections'] = $this->mresearch->getsections();
                            $this->design();
                            
                            $title = 'Research Sections - ORPMS';
                            $page = 'pages/admin/section';
                        }
                        break;
                    default:
                        redirect(base_url().'error/errors?code=404','refresh');
                }
                
                $this->template->title($title);
                $this->template->build($page,$data);
            } else {
                redirect(base_url().'error/errors?code=403','refresh');
            }
        } else {
            redirect(base_url().'account/login','refresh');
        }
    }
}